/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Grabacion;

import Vista.configuracion.NameVideo;
import Vista.configuracion.Settings;
import com.xuggle.mediatool.IMediaWriter;
import com.xuggle.mediatool.ToolFactory;
import com.xuggle.xuggler.ICodec;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.concurrent.TimeUnit;

public class VideoTest {
    private static final double FRAME_RATE = 50;
    private static Dimension screenBounds;
    public static int in = 0;
    public static final IMediaWriter writer = ToolFactory.makeWriter("video.mp4");
    public static boolean isRec = false;
    public static Thread RecordVThread = new Thread() {

        @Override
        public void run() {
            while (true) {
                if (isRec) {
                    // let's make a IMediaWriter to write the file.
                    screenBounds = Toolkit.getDefaultToolkit().getScreenSize();
                    // We tell it we're going to add one video stream, with id 0,
                    // at position 0, and that it will have a fixed frame rate of FRAME_RATE.
                    System.out.println("nombrevideo= "+NameVideo.name.toString()+"1.mp4");
                    writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_MPEG4,
                            720, 480);
                    long startTime = System.nanoTime();
                    //for (int index = 0; index < SECONDS_TO_RUN_FOR; index++) {
                    while (Settings.isState()) {
                        // take the screen shot
                        BufferedImage screen = getDesktopScreenshot();
                        //System.out.println("estado=" + Settings.state + "tomando screenshoot" + in++);
                        // convert to the right image type
                        BufferedImage bgrScreen = convertToType(screen,
                                BufferedImage.TYPE_3BYTE_BGR);
                        // encode the image to stream #0
                        writer.encodeVideo(0, bgrScreen, System.nanoTime() - startTime,
                                TimeUnit.NANOSECONDS);
                        // sleep for frame rate milliseconds
                        try {
                            Thread.sleep((long) (1000 / FRAME_RATE));
                        } catch (InterruptedException e) {
                            // ignore
                        }
                    }
                    // tell the writer to close and write the trailer if  needed
                    writer.close();
                }
            }
        }
    };

    public static BufferedImage convertToType(BufferedImage sourceImage, int targetType) {
        BufferedImage image;
        // if the source image is already the target type, return the source image
        if (sourceImage.getType() == targetType) {
            image = sourceImage;
        } // otherwise create a new image of the target type and draw the new image
        else {
            image = new BufferedImage(sourceImage.getWidth(),
                    sourceImage.getHeight(), targetType);
            image.getGraphics().drawImage(sourceImage, 0, 0, null);
        }
        return image;
    }

    public static BufferedImage getDesktopScreenshot() {
        try {
            Robot robot = new Robot();
            Rectangle captureSize = new Rectangle(screenBounds);
            return robot.createScreenCapture(captureSize);
        } catch (AWTException e) {
            e.printStackTrace();
            return null;
        }
    }
}
